home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / QuickDraw3D 1.6 SDK / Mac SampleCode New for 1.6 / ViewerCallbackSample / Source / Viewer_Events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  9.3 KB  |  559 lines  |  [TEXT/CWIE]

  1. /* 
  2.  *    Viewer_Events.c
  3.  *
  4.  *    QuickDraw 3D 1.6 Sample
  5.  *    Robert Dierkes
  6.  *
  7.  *     12/22/98    RDD        Created.
  8.  */
  9.  
  10. /*------------------*/
  11. /*    Include Files    */
  12. /*------------------*/
  13. #include "QD3D.h"
  14. #include "QD3DViewer.h"
  15.  
  16. #if defined(OS_MACINTOSH) && OS_MACINTOSH
  17. #include <MacWindows.h>
  18. #include <Events.h>
  19. #include <DiskInit.h>
  20. #include <ToolUtils.h>
  21. #endif
  22.  
  23. #include "Viewer_Error.h"
  24. #include "Viewer_Events.h"
  25. #include "Viewer_Main.h"
  26. #include "Viewer_Menu.h"
  27. #include "Viewer_Window.h"
  28. #include "Viewer_System.h"
  29.  
  30.  
  31. /*------------------*/
  32. /*      Constants        */
  33. /*------------------*/
  34.  
  35. /*----------------------*/
  36. /*    Extern Declarations    */
  37. /*----------------------*/
  38. extern    TQ3Boolean    gTimeToQuit;
  39. extern    WindowRef    gAppResizeWindow; /* For example 2 */
  40.  
  41.  
  42. /*----------------------*/
  43. /*    Global Declarations    */
  44. /*----------------------*/
  45.  
  46. /*----------------------*/
  47. /*   Type Definitions    */
  48. /*----------------------*/
  49.  
  50. /*----------------------*/
  51. /*   Local Prototypes   */
  52. /*----------------------*/
  53. static
  54. Boolean Events_HandleViewerEvents(
  55.             EventRecord                *pEvent);
  56.  
  57. static
  58. OSErr    Events_MouseDown(
  59.             EventRecord                *pEvent);
  60.  
  61. static
  62. OSErr    Events_KeyPress(
  63.             EventRecord                *pEvent);
  64.  
  65. static
  66. OSErr    Events_UpdateWindow(
  67.             EventRecord                *pEvent);
  68.  
  69. static
  70. OSErr    Events_Disk(
  71.             EventRecord                *pEvent);
  72.  
  73. static
  74. OSErr    Events_Activate(
  75.             EventRecord                *pEvent);
  76.  
  77. static
  78. OSErr    Events_ClickInContent(
  79.             EventRecord                *pEvent,
  80.             WindowPtr                pWindow);
  81.  
  82. static
  83. OSErr    Events_ClickInDrag(
  84.             EventRecord                *pEvent,
  85.             WindowPtr                window);
  86.  
  87. //TODO    #if 0 /* SAVE */
  88. static
  89. OSErr    Events_ClickInGrowBox(
  90.             EventRecord                *pEvent,
  91.             WindowPtr                pWindow);
  92. #if 0 /* SAVE */
  93. static
  94. OSErr    Events_ClickInZoomBox(
  95.             EventRecord                *pEvent,
  96.             WindowPtr                pWindow,
  97.             short                    windowPart);
  98. #endif /* SAVE */
  99.  
  100. static
  101. OSErr    Events_ClickInGoAway(
  102.             EventRecord                *pEvent,
  103.             WindowPtr                *hWindow);
  104.  
  105.  
  106. /*
  107.  *    Events_Initialize
  108.  */
  109. Boolean Events_Initialize(
  110.             void)
  111. {
  112.     FlushEvents(everyEvent, 0);
  113.  
  114.     return true;
  115. }
  116.  
  117.  
  118. /*
  119.  *    Events_Process
  120.  */
  121. void Events_Process(
  122.             void)
  123. {
  124.     EventRecord     event;
  125.     OSErr            theErr        = noErr;
  126.     Boolean            doEvent        = false;
  127.     Boolean            wasHandled    = false;
  128.  
  129.     while (! gTimeToQuit) {
  130.         /* Is there an event in the queue? */
  131.         doEvent = WaitNextEvent(everyEvent, &event, 0, NULL);
  132.  
  133.         wasHandled = Events_HandleViewerEvents(&event);
  134.  
  135.         if (! wasHandled) {
  136.             /* Viewer didn't handle event so do it here */
  137.             switch (event.what) {
  138.                 case nullEvent:
  139.                     break;
  140.  
  141.                 case mouseDown:
  142.                     theErr = Events_MouseDown(&event);
  143.                     break;
  144.  
  145.                 case keyDown:
  146.                 case autoKey:
  147.                     theErr = Events_KeyPress(&event);
  148.                     break;
  149.  
  150.                 case updateEvt:
  151.                     theErr = Events_UpdateWindow(&event);
  152.                     break;
  153.  
  154.                 case diskEvt:
  155.                     theErr = Events_Disk(&event);
  156.                     break;
  157.  
  158.                 case activateEvt:
  159.                     theErr = Events_Activate(&event);
  160.                     break;
  161.  
  162.                 case mouseUp:
  163.                 case keyUp:
  164.                 case osEvt:
  165.                 case kHighLevelEvent:
  166.                     break;
  167.             }
  168.         }
  169.         else {
  170.             /* Viewer already handled event */
  171.         }
  172.     }
  173. }
  174.  
  175.  
  176. #pragma mark -
  177.  
  178. /*
  179.  *    Events_HandleViewerEvents
  180.  */
  181. static
  182. Boolean Events_HandleViewerEvents(
  183.             EventRecord *pEvent)
  184. {
  185.     WindowPtr        pWindow            = NULL;
  186.     TQ3ViewerObject    viewer            = NULL;
  187.     Boolean            wasHandled        = false;
  188.     Boolean            cursorChanged    = false;
  189.     Point            mousePt;
  190.  
  191.     /* Get front window */
  192.     if ((pWindow = FrontWindow()) == NULL) {
  193.         return false;
  194.     }
  195.  
  196.     /* Get Viewer reference from window's refcon */
  197.     if ((viewer = Window_GetViewer(pWindow)) == NULL) {
  198.         return false;
  199.     }
  200.  
  201.     /* Let Viewer handle this event */
  202.     wasHandled = Q3ViewerEvent(viewer, pEvent);
  203.  
  204.     /* Update cursor for current mouse location */
  205.     GetMouse(&mousePt);
  206.       cursorChanged = Q3ViewerAdjustCursor(viewer, &mousePt);
  207.  
  208.     return wasHandled;
  209. }
  210.  
  211.  
  212. /*
  213.  *    Events_UpdateWindow
  214.  */
  215. OSErr Events_UpdateWindow(
  216.             EventRecord *pEvent)
  217. {
  218.     OSErr            theErr = noErr;
  219.     WindowPtr        pUpdateWindow    = NULL;
  220.     GrafPtr            savedPort        = NULL;
  221.     TQ3ViewerObject    viewer            = NULL;
  222.  
  223.     if ((pUpdateWindow = (WindowPtr) pEvent->message) == NULL) {
  224.         return paramErr;
  225.     }
  226.  
  227.     if ((viewer = Window_GetViewer(pUpdateWindow)) == NULL) {
  228.         return paramErr;
  229.     }
  230.  
  231.     /* Save port and set to Viewer's */
  232.     GetPort(&savedPort);
  233.     SetPort(pUpdateWindow);
  234.  
  235.     BeginUpdate(pUpdateWindow);
  236.  
  237.         /* Update the Viewer model and button control strip */
  238.         theErr = Q3ViewerDraw(viewer);
  239.  
  240.     EndUpdate(pUpdateWindow);
  241.  
  242.     /* Restore port */
  243.     SetPort(savedPort);
  244.  
  245.     return theErr;
  246. }
  247.  
  248.  
  249. /*
  250.  *    Events_MouseDown
  251.  */
  252. static
  253. OSErr Events_MouseDown(
  254.             EventRecord *pEvent)
  255. {
  256.     OSErr            theErr = noErr;
  257.     WindowPtr        pWindow = NULL;
  258.     short            windowPart;
  259.  
  260.     windowPart = FindWindow(pEvent->where, &pWindow);
  261.  
  262.     switch (windowPart) {
  263.     case inMenuBar: 
  264.         theErr = Menu_Command(MenuSelect (pEvent->where));
  265.         break;
  266.  
  267.     case inSysWindow:
  268.         SystemClick(pEvent, pWindow);
  269.         break;
  270.  
  271.     case inContent:
  272.         if (FrontWindow() != pWindow) {
  273.             SelectWindow(pWindow);
  274.         }
  275.         theErr = Events_ClickInContent(pEvent, pWindow);
  276.     break;
  277.  
  278.     case inDrag:
  279.         theErr = Events_ClickInDrag(pEvent, pWindow);
  280.         break;
  281.  
  282.     case inGrow:
  283. //TODO
  284. if(1){//TODO        if (pWindow == gAppResizeWindow) {
  285.             theErr = Events_ClickInGrowBox(pEvent, pWindow);
  286.         }
  287.         else {
  288.               /*
  289.                * Handllng mouse down events in the Viewer's grow box are controlled
  290.                * by setting the Viewer's kQ3ViewerDrawGrowBox flag.
  291.                */
  292.               theErr = noErr;
  293.         }
  294. //TODO
  295. #if 0 /* SAVE */
  296.         theErr = Events_ClickInGrowBox(pEvent, pWindow);
  297. //#else
  298.           /*
  299.            * Handllng mouse down events in the Viewer's grow box are controlled
  300.            * by setting the Viewer's kQ3ViewerDrawGrowBox flag.
  301.            */
  302.           theErr = noErr;
  303. #endif /* SAVE */
  304.         break;
  305.  
  306.     case inGoAway:
  307.         theErr = Events_ClickInGoAway(pEvent, &pWindow);
  308.         break;
  309.  
  310.     case inZoomIn:
  311.     case inZoomOut:
  312. #if 0 /* SAVE */
  313.         theErr = Events_ClickInZoomBox(pEvent, pWindow, windowPart);
  314. #else
  315.           theErr = noErr;
  316. #endif /* SAVE */
  317.         break;
  318.  
  319.     case inCollapseBox:
  320.           theErr = noErr;
  321.         break;
  322.  
  323.     default:
  324.           theErr = paramErr;
  325.         break;
  326.     }
  327.  
  328.     return theErr;
  329. }
  330.  
  331.  
  332. /*
  333.  *    Events_KeyPress
  334.  */
  335. static
  336. OSErr Events_KeyPress(
  337.             EventRecord        *pEvent)
  338. {
  339.     OSErr        theErr = noErr;
  340.     char        charCode;
  341.  
  342.     charCode = ((pEvent->message) & charCodeMask);
  343.  
  344.     if (pEvent->modifiers & btnState) {
  345.         /* Button is UP with a key */
  346.         if (pEvent->modifiers & cmdKey) {
  347.             theErr = Menu_Command(MenuKey(charCode));
  348.         }
  349.         else {
  350.         }
  351.     }
  352.     else {
  353.         /* Button is DOWN with a key */
  354.     }
  355.  
  356.     return theErr;
  357. }
  358.  
  359.  
  360. /*
  361.  *    Events_Disk
  362.  */
  363. static
  364. OSErr Events_Disk(
  365.             EventRecord *pEvent)
  366. {
  367.     OSErr        theErr = noErr;
  368.     Point        topLeft;
  369.  
  370.     if ((theErr = HiWord(pEvent->message)) != noErr) {
  371.         return theErr;
  372.     }
  373.  
  374.     SetPt(&topLeft, 100, 100);
  375.     (void) DIBadMount(topLeft, pEvent->message);
  376.  
  377.     return theErr;
  378. }
  379.  
  380.  
  381. /*
  382.  *    Events_Activate
  383.  */
  384. static
  385. OSErr Events_Activate(
  386.             EventRecord    *pEvent)
  387. {
  388.     OSErr        theErr = noErr;
  389.     WindowPtr    pActiveWindow;
  390.  
  391.     if ((pActiveWindow = (WindowPtr) pEvent->message) == NULL) {
  392.         return paramErr;
  393.     }
  394.  
  395.     /* Activate window */
  396.     if (pEvent->modifiers & activeFlag)
  397.     {
  398.         /* Enable/Disable items */
  399.         SetPort (pActiveWindow);
  400.     }
  401.     else
  402.     /* Deactivate window */
  403.     {
  404.         /* Enable/Disable items */
  405.     }
  406.  
  407.     return theErr;
  408. }
  409.  
  410.  
  411. #pragma mark -
  412.  
  413. /*
  414.  *    Events_ClickInContent
  415.  */
  416. static
  417. OSErr Events_ClickInContent(
  418.             EventRecord        *pEvent,
  419.             WindowPtr        pWindow)
  420. {
  421.     TQ3ViewerObject    viewer = NULL;
  422.     Boolean            wasHandled = false;
  423.     GrafPtr            savedPort;
  424.  
  425.     if ((viewer = Window_GetViewer(pWindow)) == NULL) {
  426.         return paramErr;
  427.     }
  428.  
  429.     /*
  430.      * There is a bug in versions 1.0.4 and earlier of the Viewer,
  431.      * so the port has to be set and restored.
  432.      */
  433.     GetPort(&savedPort);
  434.     SetPort((GrafPtr) pWindow);
  435.  
  436.     /* Handle mouse click inside Viewer */
  437.     wasHandled = Q3ViewerEvent(viewer, pEvent);
  438.  
  439.     SetPort(savedPort);
  440.  
  441.     return noErr;
  442. }
  443.  
  444.  
  445. /*
  446.  *    Events_ClickInDrag
  447.  */
  448. static
  449. OSErr Events_ClickInDrag(
  450.             EventRecord        *pEvent,
  451.             WindowPtr        window)
  452. {
  453.     GrafPtr            savedPort;
  454.     RgnHandle        tempRgn;
  455.     Rect            dragRect;
  456.  
  457.     GetPort(&savedPort);
  458.     SetPort((GrafPtr) window);
  459.  
  460.      tempRgn = GetGrayRgn();
  461.       dragRect = (**tempRgn).rgnBBox;
  462.  
  463.       DragWindow(window, pEvent->where, &dragRect);
  464.  
  465.     SetPort(savedPort);
  466.  
  467.     return noErr;
  468. }
  469.  
  470.  
  471. //TODO    #if 0 /* SAVE */
  472. /*
  473.  *    Events_ClickInGrowBox
  474.  */
  475. static
  476. OSErr Events_ClickInGrowBox (
  477.             EventRecord        *pEvent,
  478.             WindowPtr        pWindow)
  479. {
  480.     long        newSize;
  481.     Rect        newRect,
  482.                 oldRect,
  483.                 screenRect;
  484.     GrafPtr        savedPort;
  485.  
  486.     screenRect = (**GetGrayRgn()).rgnBBox;
  487.     screenRect.left =
  488.     screenRect.top  = 64;
  489.  
  490.     oldRect = newRect = pWindow->portRect;
  491.  
  492.     GetPort (&savedPort);
  493.     SetPort (pWindow);
  494.  
  495.     newSize = GrowWindow (pWindow, pEvent->where, &screenRect);
  496.     if (newSize != 0) {
  497.         newRect.right    = newRect.left + ((short) newSize);
  498.         newRect.bottom    = newRect.top  + ((short) (newSize >> 16));
  499.         SizeWindow (pWindow,
  500.                     (short)(newRect.right  - newRect.left),
  501.                     (short)(newRect.bottom - newRect.top),
  502.                     true);
  503.         InvalRect (&oldRect);
  504.     }
  505.  
  506.     SetPort (savedPort);
  507.  
  508.     return noErr;
  509. }
  510.  
  511.  
  512. #if 0 /* SAVE */
  513. /*
  514.  *    Events_ClickInZoomBox
  515.  */
  516. static
  517. OSErr Events_ClickInZoomBox (
  518.             EventRecord        *pEvent,
  519.             WindowPtr        pWindow,
  520.             short            windowPart)
  521. {
  522.     Rect    bounds;
  523.     GrafPtr    savedPort;
  524.  
  525.     GetPort(&savedPort);
  526.     SetPort(pWindow);
  527.  
  528.     if (TrackBox(pWindow, pEvent->where, windowPart))
  529.     {
  530.         ZoomWindow(pWindow, windowPart, true);
  531.  
  532.         bounds = pWindow->portRect;
  533.         InvalRect(&bounds);
  534.     }
  535.  
  536.     SetPort(savedPort);
  537.  
  538.     return noErr;
  539. }
  540. #endif /* SAVE */
  541.  
  542.  
  543. /*
  544.  * Events_ClickInGoAway
  545.  */
  546. OSErr Events_ClickInGoAway(
  547.             EventRecord                *pEvent,
  548.             WindowPtr                *hWindow)
  549. {
  550.     OSErr    theErr = noErr;
  551.  
  552.     if (TrackGoAway(*hWindow, pEvent->where)) {
  553.  
  554.         theErr = Window_CloseViewer(hWindow);
  555.     }
  556.  
  557.     return theErr;
  558. }
  559.